package conduitevoiture;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Polygon;
import java.util.Date;
import java.util.Timer;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;


@SuppressWarnings("serial")
public class Fenetre extends JPanel {


	private Voiture _titine;
	private int width=1000;
	private int height=1000;
        
	public Fenetre(Voiture v) {

		this.setSize(width,height);
		ConversionDistance.ECHELLE = 100;

		_titine = v;
		this.rafraichirFenetre();
		
		this.setBorder(BorderFactory.createMatteBorder(
                1, 1, 1, 1, Color.red));
	}

	@Override
	public void paintComponent(Graphics g) {
		g.clearRect(0, 0,   this.width, this.height);
		g.setColor(Color.black);
		int[] xs = {0, -10, 10};
		int[] ys = {-10, 10, 10};
		int angle = _titine.angle();
		int rad = (int) ((Math.PI / 180) * angle);
		for(int x = 0; x < xs.length; x++) {
			xs[x] = (int) (xs[x] * Math.cos(rad) - ys[x] * Math.sin(rad));
			ys[x] = (int) (xs[x] * Math.sin(rad) + ys[x] * Math.cos(rad));
		}
		
		Polygon triangle = new Polygon(xs, ys , 3);
		Coordonnee coordonneeCourante = this._titine.getCoordonnee();
		triangle.translate((int)coordonneeCourante.getX(), (int)coordonneeCourante.getY());
		
		g.drawPolygon(triangle);
	}

	public void rafraichirFenetre()
	{
		Timer leTimer = new Timer();
		int intervalleMillisec = 1;
		TimerRafraichissement laTacheALancer = new TimerRafraichissement(this);
		leTimer.schedule(laTacheALancer, new Date(), intervalleMillisec);
	}

	public void dessinerVoiture()
	{
		if (!voitureIsInTerrain()){
			_titine.setVitesse(0);
			
		}
		this.repaint();
	}
	
	public boolean voitureIsInTerrain(){
		return (_titine.getCoordonnee().getX()<width && _titine.getCoordonnee().getY()<height && _titine.getCoordonnee().getX()>0 && _titine.getCoordonnee().getY()>20);
		//return false;
	}
	
}
